package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.product.pojo.param.BrandAddNewParam;
import cn.tedu.csmall.product.pojo.param.BrandUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.product.service.IBrandService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@Slf4j
@Api(tags = "06. 品牌管理模块")
@RequestMapping("/brand")
@Validated
public class BrandController {
    @Autowired
   private IBrandService brandService;

    @PostMapping("/add-new")
    @ApiOperation("添加品牌")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid BrandAddNewParam brandAddNewParam){
        log.debug("开始处理添加品牌的请求，参数为: {}",brandAddNewParam);
        brandService.addNew(brandAddNewParam);
        log.debug("处理添加品牌的请求结束，完成");

        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(1);
        jsonResult.setMessage("添加成功");
        return jsonResult;
    }
    @PostMapping("/delete")
    @ApiOperation("根据id删除品牌")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "品牌ID",required = true,dataType = "long"),

    })
    public JsonResult delete(@Range(min  = 1,message = "根据ID删除品牌失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据id删除品牌】的请求，参数：{}", id);
        brandService.delete(id);
        log.debug("处理【根据id删除品牌】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改品牌")
    @ApiOperationSupport(order = 300)
    public JsonResult updateById(@Range(min = 1,message = "根据ID更新品牌失败，请提交合法的ID值！")
                                 @RequestParam Long brandId,
                                 @Valid BrandUpdateInfoParam brandUpdateInfoParam) {
        log.debug("开始处理【修改品牌】的请求，参数：{}", brandId, brandUpdateInfoParam);
        brandService.updateById(brandId, brandUpdateInfoParam);
        log.debug("处理【修改品牌】的请求，完成！");
        return JsonResult.ok();
    }

    @GetMapping("/standard")
    @ApiOperation("根据ID查询品牌详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")
    public JsonResult getStandardById(@RequestParam @Range(min = 1, message = "请提交有效的品牌ID值！") Long id) {
        log.debug("开始处理【根据ID查询品牌详情】的请求，参数：{}", id);
        BrandStandardVO brand = brandService.getStandardById(id);
        return JsonResult.ok(brand);
    }


    @GetMapping("/list")
    @ApiOperation("查询品牌列表")
    @ApiOperationSupport(order =420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",paramType = "query")
    })
    public JsonResult list(@Range(min = 1,message = "查询品牌列表失败，请提供正确的页码值！") Integer page,String queryType) {
        log.debug("开始处理【查询品牌列表】的请求，页码：{}", page);
        if (page == null || page < 1){
            page = 1;
        }
        PageData<BrandListItemVO> pageData;
        if ("all".equals(queryType)) {
            pageData = brandService.list(page, Integer.MAX_VALUE);
        } else {
            pageData = brandService.list(page);
        }

        return JsonResult.ok(pageData);
    }




}
